<template>
  <div>
    <!-- 卡片视图 -->
  <el-card>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名" prop="name">
        <el-input v-model.number="ruleForm.name" style="width: auto"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" width="180px">
        <el-input v-model="ruleForm.password" style="width: auto"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="ruleForm.nickname" style="width: auto"></el-input>
      </el-form-item>

      <el-form-item label="头像" prop="picture">
        <el-upload
            action="http://localhost:8080/upload"
            name="picFile"
            :limit="1"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-success="handleSuccess"
            :on-remove="handleRemove">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </el-form-item>

      <el-form-item label="手机号码" prop="phone">
        <el-input v-model.number="ruleForm.phone" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="电子邮箱" prop="email">
        <el-input v-model="ruleForm.email" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input type="textarea" v-model="ruleForm.desc"></el-input>
      </el-form-item>


      <el-form-item label="管理员选择" prop="role">
        <el-select v-model="ruleForm.role" placeholder="请选择">
          <el-option label="超级管理员" value="super"></el-option>
          <el-option label="用户管理员" value="user"></el-option>
          <el-option label="商品管理员" value="commodit"></el-option>
          <el-option label="订单管理员" value="ordes"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" style="width: 250px;" :plain="true" @click="submitForm('ruleForm')">添加</el-button>
      </el-form-item>
    </el-form>

  </el-card>
  </div>
</template>

<script>
export default {
  data() {
    var validatePhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号不能为空'));
      } else {
        const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
        console.log(reg.test(value));
        if (reg.test(value)) {
          callback();
        } else {
          return callback(new Error('请输入正确的手机号'));
        }
      }
    };
    return {
      ruleForm: {
        name: '',
        password: '',
        nickname:'',
        picture:'',
        email:'',
        desc: '',
        role: '', // 角色
      },
      dialogImageUrl: '',
      dialogVisible: false,
      objClass: {
        upLoadShow: true,
        upLoadHide: false,
      },
      rules: {
        name: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码',trigger: 'blur'}
        ],
        nickname: [
          {required: true, message: '输入昵称', trigger: 'blur'}
        ],
        phone:[
          {required: true, validator: validatePhone, trigger: 'blur'},
        ],
        email: [
          {required: true, message: '请输入邮箱地址', trigger: 'blur'},
          {type: 'email', message: '请输入有效的邮箱'}
        ],
        role: [
          {required: true, message: '请选择用户角色', trigger: 'change'}
        ]

      },

    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm('此操作会将商品信息上传至服务器,是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            center: true
          }).then(() => {
            this.$message({
              type: 'success',
              message: '已提交至服务器!'
            });
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消上传'
            });
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
      //当点击删除图片时方法会执行
      //file代表要删除的文件
      //file.response代表文件上传成功时 服务器响应的数据(文件名)
      console.log("文件名="+file.response);
      //http://localhost:8080/remove?name=xxx.jpg
      axios.get("/remove?name="+file.response).then(function (response) {
        console.log("服务器图片已经删除")
      })
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    insert(){
      //得到用户输入的微博文本内容和图片名 一起提交给服务器
      if (v.product.url==""){
        alert("图片不能为空!")
        return;
      }
      axios.post("/product/insert",v.product).then(function (response) {
        alert("添加完成!");
        //history.back(); 通过返回上一页面的方法 是不会发出请求的
        //需要再次刷新才能显示最新的内容
        location.href="/admin.html"; //显示管理页面
      })
    },
    handleSuccess(response,file,fileList){
      //response=file.response
      console.log("文件上传完成, 图片名="+response);
      v.product.url = response;
    },
    goBack(){
      //返回上一页面
      history.back();
    }
  }
}

</script>

<style scoped>

</style>